package com.xiangdao.service;

import com.github.pagehelper.PageHelper;
import com.xiangdao.dao.PostMainDao;
import com.xiangdao.dao.ShareProductDao;
import com.xiangdao.model.PageBean;
import com.xiangdao.model.PostMain;
import com.xiangdao.model.ShareProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PageService {
    @Autowired
    private ShareProductDao shareProductDao;
    @Autowired
    private PostMainDao postMainDao;

    public PageBean<ShareProduct> findSPItemByPage(String title, int currentPage, int pageSize) {
        // 总记录数
        int countNums = 0;
        countNums = shareProductDao.findAllProduct().size();

        // 设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);
        ArrayList<ShareProduct> allShareProduct = null;
        allShareProduct = shareProductDao.findAllProduct();

        PageBean<ShareProduct> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setItems(allShareProduct);
        return pageData;
    }


    public PageBean<PostMain> findPMItemByPage(String title, int currentPage, int pageSize) {
        // 总记录数
        int countNums = 0;
        countNums = postMainDao.findAllPostMain().size();

        // 设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);
        ArrayList<PostMain> allPostMain = null;
        allPostMain = postMainDao.findAllPostMain();

        PageBean<PostMain> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setItems(allPostMain);
        return pageData;
    }
    public PageBean<ShareProduct> findSPItemByPageByPrice(String title, int currentPage, int pageSize) {
        // 总记录数
        int countNums = 0;
        countNums = shareProductDao.findAllProduct().size();

        // 设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);
        ArrayList<ShareProduct> allShareProduct = null;
        allShareProduct = shareProductDao.findAllProductByPrice();

        PageBean<ShareProduct> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setItems(allShareProduct);
        return pageData;
    }

    public PageBean<PostMain> findPMItemByPageBycommentNum(String title, int currentPage, int pageSize) {
        // 总记录数
        int countNums = 0;
        countNums = postMainDao.findAllPostMain().size();

        // 设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        PageHelper.startPage(currentPage, pageSize);
        ArrayList<PostMain> allPostMain = null;
        allPostMain = postMainDao.findAllPostMainByCommentNum();

        PageBean<PostMain> pageData = new PageBean<>(currentPage, pageSize, countNums);
        pageData.setItems(allPostMain);
        return pageData;
    }


}
